# coding=UTF-8
#!/usr/bin/python3

from openpyxl import load_workbook
from PIL import Image
import os
import shutil
import urllib.request
import tkinter.messagebox

def downPic(tx1,dirName,sheet,i,j):
	#print(sheet[1][j].value)
	dirName = dirName+'/'+sheet[1][j].value+'.jpeg'
	if os.path.exists(dirName):
		tx1.insert('end', '❤')
	else:
		tx1.insert('end', '♡')
		tx1.update()
		url = sheet[i][j].hyperlink.target

		with urllib.request.urlopen(url, timeout=30) as response, open(dirName, 'wb') as f_save:
			f_save.write(response.read())
			f_save.flush()
			f_save.close()
		#缩小图片
		img = Image.open(dirName)
		w,h = img.size
		print('原图尺寸: %sx%s' % (w,h))
		if w > 500 and h > 500:
			if w > h:
				img.thumbnail((500*w//h, 500))
			else:
				img.thumbnail((500, 500*h//w))
			print('新尺寸: %sx%s' % (img.size))
			img.save(dirName, 'jpeg')



def run１(window,sw,sh,tx1,file_path):
	tx1.insert('end', '\n--------------------------------------------------读取xlsx\n')
	wb = load_workbook(file_path,keep_links=True)
	sheetnames = wb.get_sheet_names()
	#print(sheetnames[0])
	tx1.insert('end', '\n你打开的是'+sheetnames[0])
	tx1.see('end')
	tx1.update()
	sheet = wb.get_sheet_by_name(sheetnames[0])
	i = 2
	if sheetnames[0] == '车辆信息':
		while sheet[i][0].value is not None:
			#print(i)
			tx1.insert('end', '\n第'+str(i-1)+'条数据.'+sheet[i][2].value+'.创建文件夹')
			tx1.see('end')
			tx1.update()
			dirName = '车辆信息/'+sheet[i][2].value+sheet[i][3].value
			os.makedirs(dirName, exist_ok=True)
			tx1.insert('end', '[成功].下载图片:')
			tx1.see('end')
			tx1.update()
			downPic(tx1,dirName,sheet,i,8)
			downPic(tx1,dirName,sheet,i,14)
			downPic(tx1,dirName,sheet,i,20)
			downPic(tx1,dirName,sheet,i,26)
			downPic(tx1,dirName,sheet,i,32)
			downPic(tx1,dirName,sheet,i,38)
			downPic(tx1,dirName,sheet,i,44)
			downPic(tx1,dirName,sheet,i,50)
			i = i+1
	
	elif sheetnames[0] == '亿达运单':
		while sheet[i][0].value is not None:
			#print(i)
			tx1.insert('end', '\n第'+str(i-1)+'条数据.'+sheet[i][4].value+'.创建文件夹')
			tx1.see('end')
			tx1.update()
			dirName = '亿达运单/'+sheet[i][1].value.strftime("%Y-%m-%d")+'-亿达-德高/'+str(sheet[i][1].value)[:19]+sheet[i][4].value
			os.makedirs(dirName, exist_ok=True)
			tx1.insert('end', '[成功],查找以前的车辆信息')
			tx1.see('end')
			tx1.update()
			if os.path.exists('车辆信息/'+sheet[i][4].value+sheet[i][6].value):		
				tx1.insert('end', '[找到],复制以前的车辆信息')
				tx1.see('end')
				tx1.update()
				shutil.copytree('车辆信息/'+sheet[i][4].value+sheet[i][6].value, dirName+'/'+sheet[i][4].value+sheet[i][6].value)
				tx1.insert('end', '[完成].下载图片:')
				tx1.see('end')
				tx1.update()
				
				downPic(tx1,dirName,sheet,i,7)
				downPic(tx1,dirName,sheet,i,13)
				downPic(tx1,dirName,sheet,i,19)
			else:
				tx1.insert('end', '[未找到]✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖请联系驾驶员提供车辆信息\n')
				tx1.see('end')
				tx1.update()
			i = i+1
	
	else:
		tx1.insert('end', '\n你打开的文件无法识别')
	tx1.insert('end', '\n\n\n--------------------------------------------------所有图片已下载完成\n')
	tkinter.messagebox.showinfo(title='提示', message='所有图片已下载完成') 
